package no.nordicsemi.android.nrfmesh.utils;

import android.util.Log;
import no.nordicsemi.android.nrfmesh.domain.DeviceStatus;

/**
 * 数据转换测试类
 * 用于验证硬件数据转换逻辑是否正确
 */
public class DataConversionTester {
    private static final String TAG = "DataConversionTester";
    
    /**
     * 测试数据转换逻辑
     */
    public static void testDataConversion() {
        Log.d(TAG, "=== 开始数据转换测试 ===");
        
        // 创建测试用的DeviceStatus
        DeviceStatus testStatus = new DeviceStatus();
        testStatus.voltage0 = 3300;  // 3.3V (以mV为单位)
        testStatus.voltage1 = 5000;  // 5.0V (以mV为单位)
        testStatus.current = 1500;   // 1.5A (以mA为单位)
        testStatus.resistance = 2;   // 2kΩ (以kΩ为单位)
        testStatus.onoff = 1;
        testStatus.soc = 85;
        testStatus.gear = 5;
        
        Log.d(TAG, "测试数据 - 原始值:");
        Log.d(TAG, "voltage0: " + testStatus.voltage0 + " mV");
        Log.d(TAG, "voltage1: " + testStatus.voltage1 + " mV");
        Log.d(TAG, "current: " + testStatus.current + " mA");
        Log.d(TAG, "resistance: " + testStatus.resistance + " kΩ");
        
        // 模拟转换逻辑
        float voltage1InV = (float) testStatus.getVoltage0() / 1000; // 转换为V
        float voltage2InV = (float) testStatus.getVoltage1() / 1000; // 转换为V
        float currentInA = (float) testStatus.getCurrent() / 1000;   // 转换为A
        float impedanceInOhm = (float) testStatus.getResistance() * 1000; // 转换为Ω
        
        Log.d(TAG, "转换后的值:");
        Log.d(TAG, "voltage1: " + voltage1InV + " V (期望: 3.3)");
        Log.d(TAG, "voltage2: " + voltage2InV + " V (期望: 5.0)");
        Log.d(TAG, "current: " + currentInA + " A (期望: 1.5)");
        Log.d(TAG, "impedance: " + impedanceInOhm + " Ω (期望: 2000)");
        
        // 验证转换结果
        boolean voltage1Correct = Math.abs(voltage1InV - 3.3f) < 0.01f;
        boolean voltage2Correct = Math.abs(voltage2InV - 5.0f) < 0.01f;
        boolean currentCorrect = Math.abs(currentInA - 1.5f) < 0.01f;
        boolean impedanceCorrect = Math.abs(impedanceInOhm - 2000f) < 0.01f;
        
        Log.d(TAG, "转换结果验证:");
        Log.d(TAG, "voltage1转换: " + (voltage1Correct ? "✅ 正确" : "❌ 错误"));
        Log.d(TAG, "voltage2转换: " + (voltage2Correct ? "✅ 正确" : "❌ 错误"));
        Log.d(TAG, "current转换: " + (currentCorrect ? "✅ 正确" : "❌ 错误"));
        Log.d(TAG, "impedance转换: " + (impedanceCorrect ? "✅ 正确" : "❌ 错误"));
        
        boolean allCorrect = voltage1Correct && voltage2Correct && currentCorrect && impedanceCorrect;
        Log.d(TAG, "总体结果: " + (allCorrect ? "✅ 所有转换都正确" : "❌ 存在转换错误"));
        
        Log.d(TAG, "=== 数据转换测试结束 ===");
    }
    
    /**
     * 测试空值处理
     */
    public static void testNullHandling() {
        Log.d(TAG, "=== 开始空值处理测试 ===");
        
        DeviceStatus nullStatus = null;
        
        // 模拟获取硬件数据的方法
        float voltage1 = getVoltage1FromHardware(nullStatus);
        float voltage2 = getVoltage2FromHardware(nullStatus);
        float current = getCurrentFromHardware(nullStatus);
        float impedance = getImpedanceFromHardware(nullStatus);
        
        Log.d(TAG, "空值处理结果:");
        Log.d(TAG, "voltage1: " + voltage1 + " (期望: 0.0)");
        Log.d(TAG, "voltage2: " + voltage2 + " (期望: 0.0)");
        Log.d(TAG, "current: " + current + " (期望: 0.0)");
        Log.d(TAG, "impedance: " + impedance + " (期望: 0.0)");
        
        boolean allZero = (voltage1 == 0.0f && voltage2 == 0.0f && current == 0.0f && impedance == 0.0f);
        Log.d(TAG, "空值处理结果: " + (allZero ? "✅ 正确返回零值" : "❌ 空值处理有问题"));
        
        Log.d(TAG, "=== 空值处理测试结束 ===");
    }
    
    // 模拟CustomControlsActivity中的方法
    private static float getVoltage1FromHardware(DeviceStatus latestDeviceStatus) {
        if (latestDeviceStatus != null) {
            float value = (float) latestDeviceStatus.getVoltage0() / 1000;
            return value;
        }
        return 0;
    }
    
    private static float getVoltage2FromHardware(DeviceStatus latestDeviceStatus) {
        if (latestDeviceStatus != null) {
            float value = (float) latestDeviceStatus.getVoltage1() / 1000;
            return value;
        }
        return 0;
    }
    
    private static float getCurrentFromHardware(DeviceStatus latestDeviceStatus) {
        if (latestDeviceStatus != null) {
            float value = (float) latestDeviceStatus.getCurrent() / 1000;
            return value;
        }
        return 0;
    }
    
    private static float getImpedanceFromHardware(DeviceStatus latestDeviceStatus) {
        if (latestDeviceStatus != null) {
            float value = (float) latestDeviceStatus.getResistance() * 1000;
            return value;
        }
        return 0;
    }
}